<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\RowActions\ConfirmAction;
use App\Admin\Repositories\UsersLeaveMsg;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\RowAction;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class UsersLeaveMsgController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UsersLeaveMsg(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->disableCreateButton();
            $grid->disableEditButton();

            $grid->column('id')->sortable();
            $grid->column('username');
            $grid->column('phone');
            $grid->column('email');
            $grid->column('content');
            $grid->column('status', '处理状态')->using(UsersLeaveMsg::status())->label(['1' => 'success']);
            // $grid->column('ip');
            $grid->column('created_at');
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                // prepend一个操作
                if ($actions->row->status == 0) {
                    $actions->prepend(new ConfirmAction);
                }
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('username');
                $filter->like('phone');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UsersLeaveMsg(), function (Show $show) {
            $show->disableEditButton();
            $show->field('id');
            $show->field('username');
            $show->field('phone');
            $show->field('email');
            $show->field('content');
            $show->field('status')->using(UsersLeaveMsg::status())->dot(['1' => 'success']);
            $show->field('ip');
            $show->field('created_at');
            // $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UsersLeaveMsg(), function (Form $form) {
            $form->display('id');
            $form->text('username');
            $form->text('phone');
            $form->text('email');
            $form->text('content');
            $form->text('status');
            // $form->text('ip');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
